package com.globalresources.ui.struts.action.document;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import com.ascertia.adss.client.api.*;
import com.ascertia.adss.client.api.signing.*;

import java.io.*;
import java.net.*;

import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;

import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;


import com.globalresources.business.inface.IDocumentService;
import com.globalresources.business.inface.IRelationService;
import com.globalresources.business.inface.IUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.Constant;
import com.globalresources.commons.Pager;
import com.globalresources.commons.PagerServiceForDocument;
import com.globalresources.commons.PagerService;
import com.globalresources.commons.StringHelper;
import com.globalresources.po.Document;
import com.globalresources.po.Relation;
import com.globalresources.po.User;
import com.globalresources.commons.SignUtil;
import com.globalresources.commons.PDFOperate;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Jul 19, 2009 2:12:47 PM
 */
public class DocumentAction extends AbstractAction implements ServletRequestAware{	
	private PagerServiceForDocument pagerServiceForDocument;
	private PagerService pagerService;
	private IDocumentService documentService;
	private IUserService userService;
	private List<Document> noView_documents;
	private String welcomeString;
	private List<Document> unSign_documents;
	
	protected User user;
	protected Pager noView_pager;
	protected String noView_pagerMethod;
	protected String noView_totalRow;
	protected String noView_currentPage;
	
	protected Pager unSign_pager;
	protected String unSign_pagerMethod;
	protected String unSign_totalRow;
	protected String unSign_currentPage;
	
	protected Pager Signed_pager;
	protected String Signed_pagerMethod;
	protected String Signed_totalRow;
	protected String Signed_currentPage;
	protected List<Document> Signed_documents;
	
	protected Document document;
	protected String documentId;
	
	protected IRelationService relationService;
	protected List<Relation> users;
	protected List<Relation> operators;
	protected List<Relation> patients;
	
	//show document方法所需的分页变量
	//显示没有签名的程序
	protected Pager unSignPagerForShow;
	protected String unSignPagerMethodForShow;
	protected String unSignTotalRowForShow;
	protected String unSignCurrentPageForShow;
	//显示已经签名的程序
	protected Pager signedPagerForShow;
	protected String signedPagerMethodForShow;
	protected String signedTotalRowForShow;
	protected String signedCurrentPageForShow;
	
	
	//hospital's documents
	protected Pager hospitalnosigned_pager;
	protected String hospitalnosigned_pagerMethod;
	protected String hospitalnosigned_totalRow;
	protected String hospitalnosigned_currentPage;
	private List<Document> hospitalnosigned_documents;
	
	
	protected Pager hospitalsigned_pager;
	protected String hospitalsigned_pagerMethod;
	protected String hospitalsigned_totalRow;
	protected String hospitalsigned_currentPage;
	private List<Document> hospitalsigned_documents;
	
	
	//document checkbox

	private String[] documentCheckBox;
	private List<Document> unSignDocumentEditList;
	
	
	private String filenames;
	protected String[] filenameArray;
	private String message;
	private int success_count;
	private int error_count;
	//签名原因和签名位置
	private String signReason;
	private String signLocation;
	//签名成功的文档和签名失败的文档
	private List<Document> successSignDocuments;
	private List<Document> failureSignDOcuments;
	
	private HttpServletRequest request;
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}	
	
	private String filePath;
	
	Logger logger = Logger.getLogger(this.getClass());
	
	public String load(){
		users = relationService.getRelations(null, Constant.AUTHORITY_USER);
		operators = relationService.getRelations(null, Constant.AUTHORITY_OPERATOR);
		if (StringHelper.isNotEmpty(documentId)) {
			document = documentService.getDocument(new Integer(documentId));
		}else{
			document = new Document();
		}
		return SUCCESS;
	}
	
	public String save(){
		if (null != document) {
			document.setCreateTime(new Date());
			document.setIsSign(Constant.BOOLEAN_FALSE);
			document.setIsView(Constant.BOOLEAN_FALSE);
			documentService.saveDocument(document);
		}
		return SUCCESS;
	}
	
	public String list(){
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails ud = (UserDetails)obj;
			List<User> list = userService.getUser(ud.getUsername());
			if (null != list && list.size() > 0) {
				user = (User)list.get(0);
				setWelcomeString(makupWelcomeString(user));
			}
		}
		return SUCCESS;
	}
	
	public String listNoView(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		int _noView_totalRow = documentService.countDocuments(user.getDetailUser().getId(), null, Constant.BOOLEAN_FALSE);
		noView_pager = pagerService.getPager(this.getNoView_currentPage(), this.getNoView_pagerMethod(), _noView_totalRow);
		this.setNoView_currentPage(String.valueOf(noView_pager.getCurrentPage()));
		this.setNoView_totalRow(String.valueOf(_noView_totalRow));
		List<Document> _noView_documents = documentService.getDocuments(user.getDetailUser().getId(), null, Constant.BOOLEAN_FALSE);
		setNoView_documents(_noView_documents);
		return SUCCESS;
	}
	
	/**
	 * 列出未签名的文档
	 * 
	 * */
	public String listUnSignDocuments(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		int _unSign_totalRow = documentService.countDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_FALSE, null);
		unSign_pager = pagerService.getPager(this.getUnSign_currentPage(), this.getUnSign_pagerMethod(), _unSign_totalRow);
		this.setUnSign_currentPage(String.valueOf(unSign_pager.getCurrentPage()));
		this.setUnSign_totalRow(String.valueOf(_unSign_totalRow));
		List<Document> _unSign_dcouments = documentService.getDocumentsByPager(user.getDetailUser().getId(), Constant.BOOLEAN_FALSE, null,Integer.parseInt(unSign_currentPage),unSign_pager.getPageSize());
		setUnSign_documents(_unSign_dcouments);
		return SUCCESS;
	}
	
       
	/**author songduanlei@gmail.com
	 * 从list UnSignDocuments到ShowUnSignDocument
	 * showUnsignDocumentFirst
	 */
	public String showUnsignDocumentFirst(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		//System.out.println("***************fdfgdfgfdgfdgfd******************************************");
		String id=request.getParameter("id");
		setDocument(documentService.getDocument(Integer.parseInt(id)));
	//	System.out.println(document.getId());
		
		int _unSignTotalRowForShow = documentService.countDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_FALSE, null);
		//this.setUnSignCurrentPageForShow(String.valueOf(unSignPagerForShow.getCurrentPage()));
		this.setUnSignTotalRowForShow(String.valueOf(_unSignTotalRowForShow));
		List<Document> _unSignDocuments = documentService.getDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_FALSE, null);
		int _unSignCurrentPageNumber=documentService.getPageNumber(_unSignDocuments,document);
		this.setUnSignCurrentPageForShow(String.valueOf(_unSignCurrentPageNumber));
		if(document.getIsView().byteValue()==Constant.BOOLEAN_FALSE){
		document.setIsView(Constant.BOOLEAN_TRUE);
		documentService.updateDocument(document);
		}
		return SUCCESS;
		
	}
	
	/**author songduanlei@gmail.com
	 * 从list UnSignDocuments到ShowUnSignDocument
	 * showUnsignDocumentFirst
	 */
	public String showSignedDocumentFirst(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		//System.out.println("***************fdfgdfgfdgfdgfd******************************************");
		String id=request.getParameter("id");
		setDocument(documentService.getDocument(Integer.parseInt(id)));
	//	System.out.println(document.getId());
		
		int _SignedTotalRowForShow = documentService.countDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_TRUE, null);
		//this.setUnSignCurrentPageForShow(String.valueOf(unSignPagerForShow.getCurrentPage()));
		this.setSignedTotalRowForShow(String.valueOf(_SignedTotalRowForShow));
		List<Document> _SignedDocuments = documentService.getDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_TRUE, null);
		int _SignedCurrentPageNumber=documentService.getPageNumber(_SignedDocuments,document);
		this.setSignedCurrentPageForShow(String.valueOf(_SignedCurrentPageNumber));
		if(document.getIsView().byteValue()==Constant.BOOLEAN_FALSE){
		document.setIsView(Constant.BOOLEAN_TRUE);
		documentService.updateDocument(document);
		}
		return SUCCESS;
		
	}
	
	
	
	
	
	/**@author songduanlei@gmail.com
	 * 翻页查看没有签名的单个文档
	 *  
	 * */
	public String showUnsignDocument(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		int _unSignTotalRowForShow = documentService.countDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_FALSE, null);
		unSignPagerForShow = pagerServiceForDocument.getPager(this.getUnSignCurrentPageForShow(), this.getUnSignPagerMethodForShow(), _unSignTotalRowForShow);
		this.setUnSignCurrentPageForShow(String.valueOf(unSignPagerForShow.getCurrentPage()));
		this.setUnSignTotalRowForShow(String.valueOf(_unSignTotalRowForShow));
//		System.out.println(unSignPagerForShow.getCurrentPage());
		//pageSize==1 每页显示一行
		List<Document> _unSignDocuments = documentService.getDocumentsByPager(user.getDetailUser().getId(), Constant.BOOLEAN_FALSE, null,Integer.parseInt(unSignCurrentPageForShow),1);
		//setUnSign_documents(_unSign_dcouments);
		setDocument(_unSignDocuments.get(0));
		if(document.getIsView().byteValue()==Constant.BOOLEAN_FALSE){
		document.setIsView(Constant.BOOLEAN_TRUE);
		documentService.updateDocument(document);
		}
		return SUCCESS;		
	}
	
	/****
	 * @author songduanlei@gmail.com
	 * 编辑将要签名的程序
	 */
	
	public String unSignDocumentEdit(){		
      unSignDocumentEditList=documentService.getDocumentEditList(documentCheckBox);			
	  return SUCCESS;		
	}
	
	
	/**@author songduanlei@gmail.com
	 * 查看已经签名单个文档
	 *  
	 * */
	public String showSignedDocument(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		
		int _signedTotalRowForShow = documentService.countDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_TRUE, null);
		signedPagerForShow = pagerServiceForDocument.getPager(this.getSignedCurrentPageForShow(), this.getSignedPagerMethodForShow(), _signedTotalRowForShow);
		this.setSignedCurrentPageForShow(String.valueOf(signedPagerForShow.getCurrentPage()));
		this.setSignedTotalRowForShow(String.valueOf(_signedTotalRowForShow));
		List<Document> _signedDocuments = documentService.getDocumentsByPager(user.getDetailUser().getId(), Constant.BOOLEAN_TRUE, null,Integer.parseInt(unSignCurrentPageForShow),unSignPagerForShow.getPageSize());
		//setUnSign_documents(_unSign_dcouments);
		setDocument(_signedDocuments.get(0));
		return SUCCESS;
		
	}
	/**
	 * 
	 * @author songduanlei@gmail.com
	 * 
	 * view Document
	 * 
	 */
	public String viewDocument(){		
		logger.info(filenames);
		//System.out.println("*****************************************");
		//System.out.println(filenames);
		if (StringHelper.isNotEmpty(filenames)) {
			filenameArray = filenames.split(Constant.FILE_NAME_SPLITER);
		}		
		return SUCCESS;
		
	}
	/**
	 * 实施签名方法
	 * @return songduanlei@gmail.com	 * 
	 * list signDocuments
	 * 
	 */
	public String signDocument()throws Exception{	
		 
		if(documentCheckBox.length==0){			
			message="please select sign documents";
			return ERROR;
		}		
		
	  if(StringHelper.isNotEmpty(signReason)||StringHelper.isNotEmpty(signLocation)){
			   List<Document> signingDocuments=documentService.getDocumentEditList(documentCheckBox);
			   List<Document> sDocuments=new ArrayList<Document>();
			   List<Document> fDocuments=new ArrayList<Document>();
			   String userPath,tempFilePath,profile;
			   String adssServer="http://localhost:8777";
			   int pageNumber;
			   
			   
			  // System.out.println("signDocument+size"+signingDocuments.size());
			   for(int i=0;i<signingDocuments.size();i++){
				  document=signingDocuments.get(i);
				    //System.out.println("################################"+document.getFilename());
				   userPath=filePath+"users"+Constant.fs+document.getDetailUserByUserId().getId()+Constant.fs;
				   tempFilePath=filePath+"users"+Constant.fs+document.getDetailUserByUserId().getId()+Constant.fs+document.getFilename();
				   pageNumber=PDFOperate.getPdfPageCount(tempFilePath);
				  
				   boolean flag;
				   System.out.println("pageNumber="+pageNumber);
				 //  adssServer, userPath, document.getFilename(), SignUtil.getSignProfile(pageNumber), signReason, signLocation)
				   
				    System.out.println(userPath);
				    System.out.println(userPath);
				   // profile="adss:signing:profile:"+SignUtil.getSignProfile(pageNumber);
				   // System.out.println("profile="+profile);
					try{
					    EmptySignatureFieldRequest obj_emptySigFieldRequest = new EmptySignatureFieldRequest("samples_test_client", userPath+document.getFilename());
					    obj_emptySigFieldRequest.setProfileId("adss:signing:profile:002");
					    obj_emptySigFieldRequest.setSigningInfo("adss:signing:profile:001", "samples_test_signing_certificate");
					    obj_emptySigFieldRequest.overrideProfileAttribute(EmptySignatureFieldRequest.SIGNING_REASON, signReason);
					    obj_emptySigFieldRequest.overrideProfileAttribute(EmptySignatureFieldRequest.SIGNING_LOCATION, signLocation);
					    /* Writing request to disk */
					    obj_emptySigFieldRequest.writeTo("/opt/java/xml/CreateEmptySigFields-request.xml");

					    /* Sending the above constructed request to the ADSS server */
					    EmptySignatureFieldResponse obj_emptySigFieldResponse = (EmptySignatureFieldResponse) obj_emptySigFieldRequest.send(adssServer + "/adss/signing/esi");
					    /* Writing response to disk */
					    obj_emptySigFieldRequest.writeTo("/opt/java/xml/CreateEmptySigFields-response.xml");
					    /* Parsing the response */
					    if (obj_emptySigFieldResponse.isSuccessfull()) {
					      obj_emptySigFieldResponse.publishDocument(userPath+"signed"+Constant.fs+document.getFilename());		    
					     flag=true;
					    }
					    else {

					      flag=false;
					    }
					
					}catch(Exception e){
						flag=false;
					}
				   
				   if(flag)
				   {  
					  document.setIsSign(Constant.BOOLEAN_TRUE);	
					  documentService.updateDocument(document);
					  sDocuments.add(document);
					 // successSignDocuments.add(document);
					   
				   }else{					   
					  fDocuments.add(document);
				   }				   
				   successSignDocuments=sDocuments;
				   failureSignDOcuments=fDocuments;
			   }
			   return SUCCESS;
		   }else{			   
			   message="please input the sign reason and location";
			   return ERROR;
		   }
		
	}
	
	 /**
     * reads file from the given input stream and returns the contents as byte array.
     * @param a_objStream InputStream input stream to read
     * @return byte[] stream bytes
     */
    private static byte[] readStream(InputStream a_objStream) throws Exception {
        ByteArrayOutputStream obj_baos = new ByteArrayOutputStream();
        byte[] byteA_buffer = new byte[1024];
        int i_bytesRead = a_objStream.read(byteA_buffer);
        while( i_bytesRead != -1 ){
            obj_baos.write(byteA_buffer, 0, i_bytesRead);
            i_bytesRead = a_objStream.read(byteA_buffer);
        }
        return obj_baos.toByteArray();
    }//end readStream
	
	/**
	 * 
	 * @return liming.yu(yulm88@gmail.com
	 * 
	 * noSigned
	 * 
	 */
	public String listNoSignedHospitalDocs(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		
		int _totalRow = documentService.countHospitalDocuments(user.getDetailUser().getHospital().getId(),Constant.BOOLEAN_FALSE, null);
	//	System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
	//	System.out.println(_totalRow);
	//	System.out.println(user.getDetailUser().getHospital().getId());
		//System.out.println(user.getUsername());
		//System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
		hospitalnosigned_pager = pagerService.getPager(this.getHospitalnosigned_currentPage(),this.getHospitalnosigned_pagerMethod(), _totalRow);
		this.setHospitalnosigned_currentPage(String.valueOf(hospitalnosigned_pager.getCurrentPage()));
		this.setHospitalnosigned_totalRow(String.valueOf(_totalRow));
		this.hospitalnosigned_documents = documentService.getHospitalDocuments(user.getDetailUser().getHospital().getId(), Constant.ISNOSIGNED, null);
		return SUCCESS;
	}
	
	
	/**
	 * 显示医院的未签名文档
	 * 
	 * **/
	
	/**author songduanlei@gmail.com
	 * 从list UnSignDocuments到ShowUnSignDocument
	 * showUnsignDocumentFirst
	 */
	public String showUnsignHospitalDocumentFirst(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		//System.out.println("***************fdfgdfgfdgfdgfd******************************************");
		String id=request.getParameter("id");
		setDocument(documentService.getDocument(Integer.parseInt(id)));
	//	System.out.println(document.getId());		
		int _unSignTotalRowForShow = documentService.countDocuments(document.getHospital().getId(), Constant.BOOLEAN_FALSE, null);
		//this.setUnSignCurrentPageForShow(String.valueOf(unSignPagerForShow.getCurrentPage()));
		this.setUnSignTotalRowForShow(String.valueOf(_unSignTotalRowForShow));
		List<Document> _unSignDocuments = documentService.getHospitalDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_FALSE, null);
		int _unSignCurrentPageNumber=documentService.getPageNumber(_unSignDocuments,document);
		this.setUnSignCurrentPageForShow(String.valueOf(_unSignCurrentPageNumber));
		if(document.getIsView().byteValue()==Constant.BOOLEAN_FALSE){
		document.setIsView(Constant.BOOLEAN_TRUE);
		documentService.updateDocument(document);
		}
		return SUCCESS;
		
	}
	
	/**
	 * 显示医院的已经签名文档
	 * 
	 * **/
	public String showSignedHospitalDocumentFirst(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		//System.out.println("***************fdfgdfgfdgfdgfd******************************************");
		String id=request.getParameter("id");
		setDocument(documentService.getDocument(Integer.parseInt(id)));
	//	System.out.println(document.getId());
		
		int _SignedTotalRowForShow = documentService.countHospitalDocuments(document.getHospital().getId(), Constant.BOOLEAN_TRUE, null);
		//this.setUnSignCurrentPageForShow(String.valueOf(unSignPagerForShow.getCurrentPage()));
		this.setSignedTotalRowForShow(String.valueOf(_SignedTotalRowForShow));
		List<Document> _SignedDocuments = documentService.getHospitalDocuments(document.getHospital().getId(), Constant.BOOLEAN_TRUE, null);
		int _SignedCurrentPageNumber=documentService.getPageNumber(_SignedDocuments,document);
		this.setSignedCurrentPageForShow(String.valueOf(_SignedCurrentPageNumber));
		return SUCCESS;
		
	}
	
	
	
	/**
	 * 
	 * @return liming.yu(yulm88@gmail.com
	 * 
	 * Signed
	 * 
	 */
	public String listSignedHospitalDocs(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		
		int _totalRow = documentService.countHospitalDocuments(
				user.getDetailUser().getHospital().getId(), Constant.ISSIGNED, null);
		this.hospitalsigned_pager = pagerService.getPager(this.getHospitalsigned_currentPage(),
				this.getHospitalsigned_pagerMethod(), _totalRow);
		this.setHospitalsigned_currentPage(String.valueOf(hospitalsigned_pager.getCurrentPage()));
		this.setHospitalsigned_totalRow(String.valueOf(_totalRow));
		this.hospitalsigned_documents = documentService.getHospitalDocuments(
				user.getDetailUser().getHospital().getId(), Constant.ISSIGNED, null);
		return SUCCESS;
	}
	
	/**
	 * 列出已经签名的文档
	 * @author songduanlei@gmail.com
	 * 	 
	 **/
	
	public String listSignedDocuments(){
		if (null == user) {
			Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if (null != obj && obj instanceof UserDetails){
				UserDetails ud = (UserDetails)obj;
				List<User> list = userService.getUser(ud.getUsername());
				user = list.get(0);
			}
		}
		int _Signed_totalRow = documentService.countDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_TRUE, Constant.BOOLEAN_TRUE);
		Signed_pager = pagerService.getPager(this.getSigned_currentPage(), this.getSigned_pagerMethod(), _Signed_totalRow);
		this.setSigned_currentPage(String.valueOf(Signed_pager.getCurrentPage()));
		this.setSigned_totalRow(String.valueOf(_Signed_totalRow));
		List<Document> _Signed_documents = documentService.getDocuments(user.getDetailUser().getId(), Constant.BOOLEAN_TRUE, Constant.BOOLEAN_TRUE);
		setSigned_documents(_Signed_documents);
		return SUCCESS;
	}
	
	/**
	 * 取得用户姓名
	 * @return string 用户姓名
	 * 
	 *
	 **/
	private String makupWelcomeString(User user){
		StringBuffer sb = new StringBuffer();
		if (null != user) {
			sb = sb.append("Welcome " +user.getDetailUser().getFamilyName()+" " + user.getDetailUser().getLastName());
		}
		return sb.toString();
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public IDocumentService getDocumentService() {
		return documentService;
	}

	public void setDocumentService(IDocumentService documentService) {
		this.documentService = documentService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String getWelcomeString() {
		return welcomeString;
	}

	public void setWelcomeString(String welcomeString) {
		this.welcomeString = welcomeString;
	}

	public List<Document> getNoView_documents() {
		return noView_documents;
	}

	public void setNoView_documents(List<Document> noView_documents) {
		this.noView_documents = noView_documents;
	}

	public List<Document> getUnSign_documents() {
		return unSign_documents;
	}

	public void setUnSign_documents(List<Document> unSign_documents) {
		this.unSign_documents = unSign_documents;
	}

	public String getNoView_pagerMethod() {
		return noView_pagerMethod;
	}

	public void setNoView_pagerMethod(String noView_pagerMethod) {
		this.noView_pagerMethod = noView_pagerMethod;
	}

	public String getNoView_totalRow() {
		return noView_totalRow;
	}

	public void setNoView_totalRow(String noView_totalRow) {
		this.noView_totalRow = noView_totalRow;
	}

	public String getNoView_currentPage() {
		return noView_currentPage;
	}

	public void setNoView_currentPage(String noView_currentPage) {
		this.noView_currentPage = noView_currentPage;
	}

	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Pager getNoView_pager() {
		return noView_pager;
	}

	public void setNoView_pager(Pager noView_pager) {
		this.noView_pager = noView_pager;
	}



	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public String getDocumentId() {
		return documentId;
	}

	public void setDocumentId(String documentId) {
		this.documentId = documentId;
	}

	public Pager getSigned_pager() {
		return Signed_pager;
	}

	public void setSigned_pager(Pager signed_pager) {
		Signed_pager = signed_pager;
	}

	public String getSigned_pagerMethod() {
		return Signed_pagerMethod;
	}

	public void setSigned_pagerMethod(String signed_pagerMethod) {
		Signed_pagerMethod = signed_pagerMethod;
	}

	public String getSigned_totalRow() {
		return Signed_totalRow;
	}

	public void setSigned_totalRow(String signed_totalRow) {
		Signed_totalRow = signed_totalRow;
	}

	public String getSigned_currentPage() {
		return Signed_currentPage;
	}

	public void setSigned_currentPage(String signed_currentPage) {
		Signed_currentPage = signed_currentPage;
	}

	public List<Document> getSigned_documents() {
		return Signed_documents;
	}

	public void setSigned_documents(List<Document> signed_documents) {
		Signed_documents = signed_documents;
	}
	
	public List<Relation> getUsers() {
		return users;
	}

	public void setUsers(List<Relation> users) {
		this.users = users;
	}

	public List<Relation> getOperators() {
		return operators;
	}

	public void setOperators(List<Relation> operators) {
		this.operators = operators;
	}

	public List<Relation> getPatients() {
		return patients;
	}

	public void setPatients(List<Relation> patients) {
		this.patients = patients;
	}

	public IRelationService getRelationService() {
		return relationService;
	}

	public void setRelationService(IRelationService relationService) {
		this.relationService = relationService;
	}

	public Pager getHospitalnosigned_pager() {
		return hospitalnosigned_pager;
	}

	public void setHospitalnosigned_pager(Pager hospitalnosigned_pager) {
		this.hospitalnosigned_pager = hospitalnosigned_pager;
	}

	public String getHospitalnosigned_pagerMethod() {
		return hospitalnosigned_pagerMethod;
	}

	public void setHospitalnosigned_pagerMethod(String hospitalnosigned_pagerMethod) {
		this.hospitalnosigned_pagerMethod = hospitalnosigned_pagerMethod;
	}

	public String getHospitalnosigned_totalRow() {
		return hospitalnosigned_totalRow;
	}

	public void setHospitalnosigned_totalRow(String hospitalnosigned_totalRow) {
		this.hospitalnosigned_totalRow = hospitalnosigned_totalRow;
	}

	public String getHospitalnosigned_currentPage() {
		return hospitalnosigned_currentPage;
	}

	public void setHospitalnosigned_currentPage(String hospitalnosigned_currentPage) {
		this.hospitalnosigned_currentPage = hospitalnosigned_currentPage;
	}

	public List<Document> getHospitalnosigned_documents() {
		return hospitalnosigned_documents;
	}

	public void setHospitalnosigned_documents(
			List<Document> hospitalnosigned_documents) {
		this.hospitalnosigned_documents = hospitalnosigned_documents;
	}

	public Pager getHospitalsigned_pager() {
		return hospitalsigned_pager;
	}

	public void setHospitalsigned_pager(Pager hospitalsigned_pager) {
		this.hospitalsigned_pager = hospitalsigned_pager;
	}

	public String getHospitalsigned_pagerMethod() {
		return hospitalsigned_pagerMethod;
	}

	public void setHospitalsigned_pagerMethod(String hospitalsigned_pagerMethod) {
		this.hospitalsigned_pagerMethod = hospitalsigned_pagerMethod;
	}

	public String getHospitalsigned_totalRow() {
		return hospitalsigned_totalRow;
	}

	public void setHospitalsigned_totalRow(String hospitalsigned_totalRow) {
		this.hospitalsigned_totalRow = hospitalsigned_totalRow;
	}

	public String getHospitalsigned_currentPage() {
		return hospitalsigned_currentPage;
	}

	public void setHospitalsigned_currentPage(String hospitalsigned_currentPage) {
		this.hospitalsigned_currentPage = hospitalsigned_currentPage;
	}

	public List<Document> getHospitalsigned_documents() {
		return hospitalsigned_documents;
	}

	public void setHospitalsigned_documents(List<Document> hospitalsigned_documents) {
		this.hospitalsigned_documents = hospitalsigned_documents;
	}

	/**
	 * @return the filenames
	 */
	public String getFilenames() {
		return filenames;
	}

	/**
	 * @param filenames the filenames to set
	 */
	public void setFilenames(String filenames) {
		this.filenames = filenames;
	}

	/**
	 * @return the filenameArray
	 */
	public String[] getFilenameArray() {
		return filenameArray;
	}

	/**
	 * @param filenameArray the filenameArray to set
	 */
	public void setFilenameArray(String[] filenameArray) {
		this.filenameArray = filenameArray;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the success_count
	 */
	public int getSuccess_count() {
		return success_count;
	}

	/**
	 * @param success_count the success_count to set
	 */
	public void setSuccess_count(int success_count) {
		this.success_count = success_count;
	}

	/**
	 * @return the error_count
	 */
	public int getError_count() {
		return error_count;
	}

	/**
	 * @param error_count the error_count to set
	 */
	public void setError_count(int error_count) {
		this.error_count = error_count;
	}

	/**
	 * @return the unSign_pager
	 */
	public Pager getUnSign_pager() {
		return unSign_pager;
	}

	/**
	 * @param unSign_pager the unSign_pager to set
	 */
	public void setUnSign_pager(Pager unSign_pager) {
		this.unSign_pager = unSign_pager;
	}

	/**
	 * @return the unSign_pagerMethod
	 */
	public String getUnSign_pagerMethod() {
		return unSign_pagerMethod;
	}

	/**
	 * @param unSign_pagerMethod the unSign_pagerMethod to set
	 */
	public void setUnSign_pagerMethod(String unSign_pagerMethod) {
		this.unSign_pagerMethod = unSign_pagerMethod;
	}

	/**
	 * @return the unSign_totalRow
	 */
	public String getUnSign_totalRow() {
		return unSign_totalRow;
	}

	/**
	 * @param unSign_totalRow the unSign_totalRow to set
	 */
	public void setUnSign_totalRow(String unSign_totalRow) {
		this.unSign_totalRow = unSign_totalRow;
	}

	/**
	 * @return the unSign_currentPage
	 */
	public String getUnSign_currentPage() {
		return unSign_currentPage;
	}

	/**
	 * @param unSign_currentPage the unSign_currentPage to set
	 */
	public void setUnSign_currentPage(String unSign_currentPage) {
		this.unSign_currentPage = unSign_currentPage;
	}

	/**
	 * @return the filePath
	 */
	public String getFilePath() {
		return filePath;
	}

	/**
	 * @param filePath the filePath to set
	 */
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public PagerServiceForDocument getPagerServiceForDocument() {
		return pagerServiceForDocument;
	}

	public void setPagerServiceForDocument(
			PagerServiceForDocument pagerServiceForDocument) {
		this.pagerServiceForDocument = pagerServiceForDocument;
	}

	/**
	 * @return the unSignPagerForShow
	 */
	public Pager getUnSignPagerForShow() {
		return unSignPagerForShow;
	}

	/**
	 * @param unSignPagerForShow the unSignPagerForShow to set
	 */
	public void setUnSignPagerForShow(Pager unSignPagerForShow) {
		this.unSignPagerForShow = unSignPagerForShow;
	}

	/**
	 * @return the unSignPagerMethodForShow
	 */
	public String getUnSignPagerMethodForShow() {
		return unSignPagerMethodForShow;
	}

	/**
	 * @param unSignPagerMethodForShow the unSignPagerMethodForShow to set
	 */
	public void setUnSignPagerMethodForShow(String unSignPagerMethodForShow) {
		this.unSignPagerMethodForShow = unSignPagerMethodForShow;
	}

	/**
	 * @return the unSignTotalRowForShow
	 */
	public String getUnSignTotalRowForShow() {
		return unSignTotalRowForShow;
	}

	/**
	 * @param unSignTotalRowForShow the unSignTotalRowForShow to set
	 */
	public void setUnSignTotalRowForShow(String unSignTotalRowForShow) {
		this.unSignTotalRowForShow = unSignTotalRowForShow;
	}

	/**
	 * @return the unSignCurrentPageForShow
	 */
	public String getUnSignCurrentPageForShow() {
		return unSignCurrentPageForShow;
	}

	/**
	 * @param unSignCurrentPageForShow the unSignCurrentPageForShow to set
	 */
	public void setUnSignCurrentPageForShow(String unSignCurrentPageForShow) {
		this.unSignCurrentPageForShow = unSignCurrentPageForShow;
	}

	/**
	 * @return the signedPagerForShow
	 */
	public Pager getSignedPagerForShow() {
		return signedPagerForShow;
	}

	/**
	 * @param signedPagerForShow the signedPagerForShow to set
	 */
	public void setSignedPagerForShow(Pager signedPagerForShow) {
		this.signedPagerForShow = signedPagerForShow;
	}

	/**
	 * @return the signedPagerMethodForShow
	 */
	public String getSignedPagerMethodForShow() {
		return signedPagerMethodForShow;
	}

	/**
	 * @param signedPagerMethodForShow the signedPagerMethodForShow to set
	 */
	public void setSignedPagerMethodForShow(String signedPagerMethodForShow) {
		this.signedPagerMethodForShow = signedPagerMethodForShow;
	}

	/**
	 * @return the signedTotalRowForShow
	 */
	public String getSignedTotalRowForShow() {
		return signedTotalRowForShow;
	}

	/**
	 * @param signedTotalRowForShow the signedTotalRowForShow to set
	 */
	public void setSignedTotalRowForShow(String signedTotalRowForShow) {
		this.signedTotalRowForShow = signedTotalRowForShow;
	}

	/**
	 * @return the signedCurrentPageForShow
	 */
	public String getSignedCurrentPageForShow() {
		return signedCurrentPageForShow;
	}

	/**
	 * @param signedCurrentPageForShow the signedCurrentPageForShow to set
	 */
	public void setSignedCurrentPageForShow(String signedCurrentPageForShow) {
		this.signedCurrentPageForShow = signedCurrentPageForShow;
	}

	/**
	 * @return the documentCheckBox
	 */
	public String[] getDocumentCheckBox() {
		return documentCheckBox;
	}

	/**
	 * @param documentCheckBox the documentCheckBox to set
	 */
	public void setDocumentCheckBox(String[] documentCheckBox) {
		this.documentCheckBox = documentCheckBox;
	}

	/**
	 * @return the unSignDocumentEditList
	 */
	public List<Document> getUnSignDocumentEditList() {
		return unSignDocumentEditList;
	}

	/**
	 * @param unSignDocumentEditList the unSignDocumentEditList to set
	 */
	public void setUnSignDocumentEditList(List<Document> unSignDocumentEditList) {
		this.unSignDocumentEditList = unSignDocumentEditList;
	}

	/**
	 * @return the signReason
	 */
	public String getSignReason() {
		return signReason;
	}

	/**
	 * @param signReason the signReason to set
	 */
	public void setSignReason(String signReason) {
		this.signReason = signReason;
	}

	/**
	 * @return the signLocation
	 */
	public String getSignLocation() {
		return signLocation;
	}

	/**
	 * @param signLocation the signLocation to set
	 */
	public void setSignLocation(String signLocation) {
		this.signLocation = signLocation;
	}

	/**
	 * @return the successSignDocuments
	 */
	public List<Document> getSuccessSignDocuments() {
		return successSignDocuments;
	}

	/**
	 * @param successSignDocuments the successSignDocuments to set
	 */
	public void setSuccessSignDocuments(List<Document> successSignDocuments) {
		this.successSignDocuments = successSignDocuments;
	}

	/**
	 * @return the failureSignDOcuments
	 */
	public List<Document> getFailureSignDOcuments() {
		return failureSignDOcuments;
	}

	/**
	 * @param failureSignDOcuments the failureSignDOcuments to set
	 */
	public void setFailureSignDOcuments(List<Document> failureSignDOcuments) {
		this.failureSignDOcuments = failureSignDOcuments;
	}


	
	
}
